<template>
	<!-- 岗位列表 -->
	<div class="content">
		<div class="content-top">
			岗位管理_岗位查询
		</div>
		<div class="content-operation">
			<el-button type="primary" icon="el-icon-plus" @click='is_add = true' v-if="has_add">添加岗位</el-button>
		</div>
		<div class="content-search">
			<el-form :inline="true" class="demo-form-inline" style="margin-top: 20px;">
				<el-form-item label="岗位名称">
					<el-input placeholder="岗位名称"></el-input>
				</el-form-item>

				<el-button type="primary" icon="el-icon-search">搜索</el-button>
				</el-form-item>
			</el-form>
		</div>
		<div class="content-table">
			<el-table :data="tableData" show-header stripe style="width: 100%;">
				<el-table-column prop="positionName" label="岗位名称" width="200">
				</el-table-column>
				<el-table-column prop="positionCode" label="岗位编码" width="200">
				</el-table-column>
				<el-table-column label="操作">
					<template slot-scope="scope">
						<el-button @click="handleClick(scope.row)" v-if="has_edit" type="text" size="small"><i class="el-icon-edit"></i></el-button>
						<el-button @click="handleClick1(scope.row)" v-if="has_del" type="text" size="small"><i class="el-icon-delete"></i></el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<div class="content-page">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="page"
			 :page-size=limit layout="prev, pager, next, jumper" :total=total>
			</el-pagination>
		</div>
		<template v-if="is_add">
			<div class="all-screen"></div>
			<div class='add-box'>
				<PositionAdd @saveAdd='saveAdd'></PositionAdd>
			</div>
		</template>
		<template v-if="is_edit">
			<div class="all-screen"></div>
			<div class='add-box'>
				<PositionEdit @saveEdit='saveEdit' :id='id'></PositionEdit>
			</div>
		</template>
	</div>
</template>

<script>
	import PositionAdd from '@/components/position/PositionAdd.vue'
	import PositionEdit from '@/components/position/PositionEdit.vue'
	import Global from '@/components/Global.vue'
	export default {
		components: {
			PositionAdd: PositionAdd,
			PositionEdit: PositionEdit,
		},
		props:['power'],
		data() {
			return {
				id: 'id',
				is_add: false,
				is_edit: false,
				tableData: [],
				limit: 10,
				page: 1,
				total: 1,
				has_add:false,
				has_edit:false,
				has_del:false
			}
		},
		mounted() {
			this.check_power();
			this.get_list();
		},
		methods: {
			check_power(){
				var self = this;
				this.power.forEach(function(element) {
					if(element.permissionName.indexOf("添加") != -1 )self.has_add = true;
					if(element.permissionName.indexOf("编辑") != -1 )self.has_edit = true;
					if(element.permissionName.indexOf("删除") != -1 )self.has_del = true;
				});
			},
			get_list() {
				var self = this;
				this.$axios.post(this.$global.serverIp + 'admin/sysPosition/findPage', {
				  "limit": self.limit + "",
				  "page": self.page + "",
				  "name": self.dept
				  })
				  .then(function (res) {
					  if(res != ''){
						  self.tableData = res.data.list;
						  self.total = res.data.totalCount;
						  self.page = res.data.currPage;
					  }
						
				  })
			},
			saveAdd(type) {
				if (type == 2) {
					this.get_list();
				}
				this.is_add = false;
			},
			saveEdit(type) {
				if (type == 2) {
					this.get_list();
				}
				this.is_edit = false;
			},
			handleSizeChange(val) {
				console.log(`每页 ${val} 条`);
			},
			handleCurrentChange(val) {
				this.page = val;
				this.get_list();
				console.log(`当前页: ${val}`);
			},
			handleClick(row) {
				console.log(row.name);
				this.id = row.id;
				this.is_edit = true;
			},
			handleClick1(row) {
				this.delopen(row);
			},
			delopen(row) {
				var self = this;
				this.$confirm('此操作将删除该岗位, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					$.ajax({
						url: Global.serverIp + 'jianeng-admin/sysPosition/delete',
						data: '["' + row.id + '"]',
						xhrFields: {
							withCredentials: true
						},
						beforeSend : function(request) {
							request.setRequestHeader("Authorization", 'Bearer '+localStorage.getItem('token'));
						},
						contentType: 'application/json;charset=utf-8',
						dataType: 'json',
						type: 'delete',
						success: function(res) {
							if (res.code == Global.success_code) {
								console.log(res);
								self.$popup.open("删除成功");
								self.get_list();
							} else {
								self.$popup.open(res.msg);
							}
						},
						error: function(res) {
							if(res.responseJSON.code == '040005'){
								self.$popup.open("登录过期");
							}else{
								self.$popup.open("请求失败");
							}
						}
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
		},

	}
	import '@/assets/css/common.css'
</script>
